--[[Todo list
choose default weapons
Get character creation working
method for returning player to login field
method for returning to 'lobby'
]]--

local checks = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
local player = getLocalPlayer()
local modelIDs = { 7, 28, 30, 47, 57, 60, 73, 82, 83, 84, 93, 91, 117, 118, 120, 121, 122, 101, 123, 128, 155, 163, 164, 165, 166, 170, 191, 193, 201, 205, 211, 215, 217, 228, 233, 227, 250, 252, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288 }
local btn_table = {}
local lbl_table = {}
local character_table = {}
local slot = 0
local selected = nil
local x, y = guiGetScreenSize()
x, y = x / 2, y / 2

function resourceStart()
	drawImage()
end

function guiAlpha(element, amount)
	local alpha = guiGetAlpha(element)
	guiSetAlpha(element, alpha-amount)
end

function setUpPlayerNames ( names )
	triggerServerEvent ( "requestPlayerNames", getLocalPlayer() )
end

function acceptCharacter ( names )
	local text = tostring ( guiGetText ( edt_Name ) )
	checks[13] = {}
	for k,v in ipairs ( names ) do
		local name = names[k].name
		local temp1 = string.lower ( name )
		local temp2 = string.lower ( text )
		if ( temp1 ~= temp2 ) then
			if checks[13].name == 0 or checks[13].name == nil then
				checks[13].name = 1
			else
				checks[13].name = checks[13].name + 1
			end
		end
	end
	if checks[13].name == #names then
		local radio = checkRadios()
		if radio == "Criminal" or radio == "Enforcer" then
			if checks[11] == 0 then
				confirmWindow = guiCreateWindow ( (x - 125), (y - 60), 280, 120, "Confirm Name and Skin", false )
				lbl_Warn = guiCreateLabel ( 15, 20, 250, 45, "All settings chosen below are FINAL! Admins cannot help you change them in the future. Be sure you have made your selections.", false, confirmWindow )
				guiLabelSetHorizontalAlign ( lbl_Warn, "center", true )
				btn_No = guiCreateButton ( 0, 70, 100, 50, "No", false, confirmWindow )
				btn_Yes = guiCreateButton ( 140, 70, 100, 50, "Yes", false, confirmWindow )
				addEventHandler ( "onClientGUIClick", btn_Yes, finalizeCharacter, false )
				addEventHandler ( "onClientGUIClick", btn_No, toggleConfirm, false )
				checks[11] = 1
			else
				toggleConfirm()
			end
			checks[13] = {}
		end
	else
		outputChatBox ( "Name taken or unavailable." )
	end
end

function checkRadios()
	local crim = guiRadioButtonGetSelected( rdo_Crim )
	local enfo = guiRadioButtonGetSelected( rdo_Enfo )
	if crim == false and enfo == false then
		outputChatBox ( "Please make a faction selection." )
	elseif crim == true then
		return "Criminal"
	else
		return "Enforcer"
	end
end

function finished()
	guiSetInputEnabled(false)
end

function finalizeCharacter()
	local skin = modelIDs[checks[1]]
	local name = guiGetText ( edt_Name )
	local faction = checkRadios()
	triggerServerEvent ( "buildCharacter", getLocalPlayer(), slot, name, skin, faction )
end

function toggleConfirm()
	if checks[6] == 0 then
		guiSetVisible ( confirmWindow, false )
		checks[6] = 1
	else
		guiSetVisible ( confirmWindow, true )
		checks[6] = 0
	end
end

function toggleSkinSelect()
	if checks[5] == 0 then
		guiSetText ( edt_Name, "" )
		guiSetVisible ( charWindow, false )
		guiSetVisible ( btn_Next, false )
		guiSetVisible ( btn_Prev, false )
		checks[5] = 1
	else
		guiSetVisible ( charWindow, true )
		guiSetVisible ( btn_Next, true )
		guiSetVisible ( btn_Prev, true )
		checks[5] = 0
	end
end

function toggleDefaults()
	if checks[4] == 0 then
		for i=1,8 do
			guiSetVisible ( btn_table[i], false )
		end
		guiSetVisible ( statsWindow, false )
		guiSetVisible ( createCharacter, false )
		checks[4] = 1
		removeEventHandler ( "onClientRender", getRootElement(), overlayG )
	else
		for i=1,8 do
			guiSetVisible ( btn_table[i], true )
		end
		guiSetVisible ( statsWindow, true )
		guiSetVisible ( createCharacter, true )
		checks[4] = 0
		addEventHandler ( "onClientRender", getRootElement(), overlayG )
	end
end

function drawImage()
	logo = guiCreateStaticImage ( (x-203), (y-116), 407, 233, "client_files/apbmta.png", false )
	guiMoveToBack ( logo )
	guiSetEnabled ( logo, false )
end

function destroyLogo()
	destroyElement ( logo )
end


function fadeLogo()
	setTimer(guiAlpha, 50, 20, logo, 0.05)
end

function cancelButt()
	toggleSkinSelect()
	toggleDefaults()
end

function skinSelect()
	checks[1] = 1
	if checks[8] == 1 then
		toggleDefaults()
		if checks[9] == 0 then
			btn_Next = guiCreateButton ( (x + 150), (y + 150), 100, 50, "Skin Next", false )
			btn_Prev = guiCreateButton ( (x - 200), (y + 150), 100, 50, "Skin Previous", false )
			charWindow = guiCreateWindow ( (x - 100), (y + 112), 250, 120, "Character Creation", false )
			rdo_Crim = guiCreateRadioButton ( 0, 20, 100, 15, "Criminal", false, charWindow )
			rdo_Enfo = guiCreateRadioButton ( 170, 20, 100, 15, "Enforcer", false, charWindow )
			guiWindowSetMovable ( charWindow, false )
			guiWindowSetSizable ( charWindow, false )
			btn_Canc = guiCreateButton ( 0, 70, 100, 50, "Cancel", false, charWindow )
			btn_Acce = guiCreateButton ( 140, 70, 100, 50, "Accept", false, charWindow )
			edt_Name = guiCreateEdit ( 0, 45, 250, 24, "Character Name", false, charWindow )
			addEventHandler ( "onclientGUIClick", edt_Name, clearTitle, false )
			addEventHandler ( "onClientGUIClick", btn_Next, alterValue, false )
			addEventHandler ( "onClientGUIClick", btn_Prev, alterValue, false )
			addEventHandler ( "onClientGUIClick", btn_Canc, cancelButt, false )
			addEventHandler ( "onClientGUIClick", btn_Acce, setUpPlayerNames, false )
			checks[9] = 1
		else
			toggleSkinSelect()
		end
	else
		if checks[12] == 0 then
			noSelectWin = guiCreateWindow ( (x - 100), (y + 112), 150, 100, "No Selection", false )
			btn_noSel = guiCreateButton ( 25, 60, 100, 30, "OK", false, noSelectWin )
			addEventHandler ( "onClientGUIClick", btn_noSel, toggleNoSel, false )
			checks[12] = 1
		else
			toggleNoSel()
		end
	end
end

function clearTitle()
	outputChatBox ("ran")
	if ( guiGetText ( edt_Name ) == "Character Name" ) then
		guiSetText ( edt_Name, "" )
	end
end

function toggleNoSel()
	if checks[7] == 0 then
		guiSetVisible ( noSelectWin, false )
		checks[7] = 1
	else
		guiSetVisible ( noSelectWin, true )
		checks[7] = 0
	end
end

function createCharacter()
	setTimer ( fadeLogo, 1000, 1 )
	ped = createPed ( modelIDs[checks[1]], 2054.75, 1198.75, 83.75 )
	local plate = createObject ( 4206, 2056, 1199.75, 83.75 )
	setElementAlpha ( plate, 0 )
	setPedRotation ( ped, 135 )
	setElementFrozen ( ped, true )
	setPedAnimation ( ped, "DANCING", "dance_loop" )
end

function displayCharacters()
	if checks[10] == 0 then
		local rx, ry = guiGetScreenSize()
		local font = guiCreateFont ( "client_files/nb.ttf", 24 )
		local font2 = guiCreateFont ( "client_files/tb.ttf", 12 )
		for i=0,350,50 do
			btn_table[checks[2]] = guiCreateButton ( rx - 105, i + 5, 100, 50, checks[2], false )
			guiSetFont ( btn_table[checks[2]], font )
			addEventHandler ( "onClientGUIClick", btn_table[checks[2]], retrieveData, false )
			checks[2] = checks[2] + 1
		end
		statsWindow = guiCreateWindow ( rx - 305, 5, 200, 400, "Character Stats", false )
		guiWindowSetMovable ( statsWindow, false )
		guiWindowSetSizable ( statsWindow, false )
		for i=23,380,23 do
			lbl_table[checks[3]] = {}
			lbl_table[checks[3]] = guiCreateLabel ( 10, i, 180, 20, "", false, statsWindow )
			guiLabelSetHorizontalAlign ( lbl_table[checks[3]], "center" )
			checks[3] = checks[3] + 1
		end
		for i=1,15,2 do
			guiSetFont ( lbl_table[i], font2 )
		end
		guiSetText ( lbl_table[1], "Character Name" )
		guiSetText ( lbl_table[3], "Skin" )
		guiSetText ( lbl_table[5], "Money" )
		guiSetText ( lbl_table[7], "Threat Level" )
		guiSetText ( lbl_table[9], "Rank" )
		guiSetText ( lbl_table[11], "Vehicle" )
		guiSetText ( lbl_table[13], "Primary Weapon" )
		guiSetText ( lbl_table[15], "Secondary Weapon" )
		for i=1,15,2 do
			guiSetFont ( lbl_table[i], font2 )
		end
		for i=2,16,2 do
			guiSetFont ( lbl_table[i], "default-bold-small" )
		end
		createCharacter = guiCreateButton ( rx-305, 400 + 5, 300, 50, "Create Character", false )
		addEventHandler ( "onClientGUIClick", createCharacter, skinSelect, false )
		checks[10] = 1
	else
		toggleDefaults()
	end
end

function setFactionSymbol()
	
end

function retrieveData()
	local value = tonumber ( guiGetText ( source ) )
	slot = value
	guiSetText ( lbl_table[2], character_table[value][1] )
	guiSetText ( lbl_table[4], character_table[value][2] )
	if character_table[value][2] == "N/a" then
		setElementModel ( ped, modelIDs[checks[1]] )
	else
		setElementModel ( ped, tonumber ( character_table[value][2] ) )
	end
	if character_table[value][3] == "N/a" then
		guiSetText ( lbl_table[6], character_table[value][3] )
	else
		guiSetText ( lbl_table[6], character_table[value][3].."$" )
	end
	guiSetText ( lbl_table[8], character_table[value][4] )
	guiSetText ( lbl_table[10], character_table[value][5] )
	guiSetText ( lbl_table[12], character_table[value][6] )
	guiSetText ( lbl_table[14], character_table[value][7] )
	guiSetText ( lbl_table[16], character_table[value][8] )
	if checks[8] == 0 then
		addEventHandler ( "onClientRender", getRootElement(), overlayG )
		checks[8] = 1
	end
	selected = source
	guiSetProperty ( source, "TextColour", "FF68F000" )
end

function overlayG()
	local sx, sy = guiGetPosition ( selected, false )
	local w, h = guiGetSize ( selected, false )
	dxDrawRectangle ( sx, sy, w, h, tocolor(0, 150, 200, 95), true )
end

function tableData ( number, name, skin, money, threat, level, vehicle, weapon1, weapon2 )
	character_table[number] = {}
	character_table[number] = { name, skin, money, threat, level, vehicle, weapon1, weapon2 }
end

function alterValue()
	local btn = guiGetText ( source )
	if btn == "Skin Next" then
		if checks[1] == #modelIDs then
			checks[1] = 1
		else
			checks[1] = checks[1] + 1
		end
	else
		if checks[1] == 1 then
			checks[1] = #modelIDs
		else
			checks[1] = checks[1] - 1
		end
	end
	setElementModel ( ped, modelIDs[checks[1]] )
end

addEvent ( "sendPlayerNames", true )
addEvent ( "displayCharacterSelect", true )
addEvent ( "sendPlayerData", true )
addEvent ( "destroyLogo", true )
addEvent ( "displayCharacters", true )
addEventHandler ( "displayCharacters", player, displayCharacters )
addEventHandler ( "destroyLogo", player, destroyLogo )
addEventHandler ( "sendPlayerData", player, tableData )
addEventHandler ( "displayCharacterSelect", player, createCharacter )
addEventHandler ( "sendPlayerNames", getRootElement(), acceptCharacter )
addEventHandler ( "onClientResourceStart", getResourceRootElement ( getThisResource() ), resourceStart )